//
//  ZHZMyStudentCty.m
//  ZHZtest
//
//  Created by 李彦宏 on 2017/12/16.
//  Copyright © 2017年 LYH. All rights reserved.
//

#import "ZHZMyStudentCty.h"
#import "KBTeachHead.h"
#import "MyStudentCell.h"
#import "StudentListModel.h"

#define MyStudentCellID @"MyStudentCell"
#define KBTeachHeadID @"KBTeachHead"
@interface ZHZMyStudentCty ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *MyStudentList;
@property(nonatomic, strong)NSMutableArray *selectedArray;//是否被点击
@property (nonatomic, strong) NSArray *cellarry;
@property(nonatomic,strong)UIView *noDataView;

@property(nonatomic,strong)  StudentListModel *model;
@property(nonatomic,strong)NSMutableArray <StudentListModel*>*stuArray;


@end

@implementation ZHZMyStudentCty
-(void)loadView
{
    [super loadView];
    //创建分组类型的tableview
    _MyStudentList = [[UITableView alloc]initWithFrame:CGRectMake(0 ,0, Screen_width, Screen_Height)style:UITableViewStyleGrouped];
    _MyStudentList.tableHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_width, 0.000001f)];
    _MyStudentList.backgroundColor=[UIColor whiteColor];
    _MyStudentList.tableFooterView = [[UIView alloc]init];
    _MyStudentList.delegate = self;
    _MyStudentList.dataSource = self;
    [self.view addSubview:_MyStudentList];
    [_MyStudentList registerNib:[UINib nibWithNibName:@"KBTeachHead" bundle:nil] forHeaderFooterViewReuseIdentifier:KBTeachHeadID];
    
    [_MyStudentList registerNib:[UINib nibWithNibName:@"MyStudentCell" bundle:nil] forCellReuseIdentifier:MyStudentCellID];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestData];
    _cellarry=[NSArray arrayWithObjects: @"本科院校与专业:",@"目标院校与专业 :",@"Q Q:",@"微信 :",@"手机:",nil];
    
}
#pragma 获取数据
-(void)requestData{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{
                           @"gid" : Gid,
                           @"gpass" : Gpassword
                           };
    [[AFNetworkTool shareManager]postJSONWithUrl:Student_List parmas:dict successData:^(id json) {
        
        NSDictionary * resuts = json;
        if ([resuts[@"message"] isEqualToString:@"success"]) {
            NSArray *listArr = json[@"data"];
            if (listArr.count>0) {
                for (int i = 0; i<listArr.count; i++) {
                    self.model = [StudentListModel yy_modelWithJSON:listArr[i]];
                    [weakSelf.stuArray addObject:self.model];
                }
                //根据网络数据的个数，循环添加self.selectedArray个数
                for (int i = 0; i<self.stuArray.count; i++) {
                    [self.selectedArray addObject:@"0"];
                }
                NSLog(@"wo>>>>>>>%@", self.selectedArray);
            }
        }
        if (self.stuArray.count> 0) {
             NSLog(@">>>>有数组");
        }else{
            [self.MyStudentList removeFromSuperview];
            [self.view addSubview:self.noDataView];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.MyStudentList reloadData];
        });
    } failure:^(NSError *error) {
        
    }];
    NSLog(@"选择学生");
}
#pragma mark cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyStudentCell *cell=[tableView dequeueReusableCellWithIdentifier:MyStudentCellID];
    cell.GoalLbe.text=_cellarry[indexPath.row];
    self.model = self.stuArray[indexPath.section];
    switch (indexPath.row) {
        case 0:
            cell.GoalLbeValue.text = self.model.college;
            break;
        case 1:
            cell.GoalLbeValue.text = self.model.goal;
            break;
        case 2:
            cell.GoalLbeValue.text = self.model.QQ;
            break;
        case 3:
            cell.GoalLbeValue.text = self.model.Winxin;
            break;
        case 4:
            cell.GoalLbeValue.text = self.model.phone;
            break;
            
        default:
            break;
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.model = self.stuArray[indexPath.section];
    if (indexPath.row == 4) {
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.model.phone];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
    }

}

#pragma mark cell的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //判断section的标记是否为1,如果是说明为展开,就返回真实个数,如果不是就说明是缩回,返回0.
    if ([_selectedArray[section] isEqualToString:@"1"]) {
        return _cellarry.count;
    }
    return 0;
}
#pragma mark section的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.stuArray.count;
}

#pragma cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

#pragma mark - section内容
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    KBTeachHead *head=[tableView dequeueReusableHeaderFooterViewWithIdentifier:KBTeachHeadID];
    head.model = self.stuArray[section];
    //手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(topgesture:)];
    [head addGestureRecognizer:tap];
    head.tag = section;
    return head;
}

-(void)topgesture:(UITapGestureRecognizer*)tap
{
    NSInteger index = tap.view.tag;
    
    if ([_selectedArray[index] isEqualToString:@"0"]) {
        
        [_selectedArray replaceObjectAtIndex:index withObject:@"1"];
        
    }else{
        [_selectedArray replaceObjectAtIndex:index withObject:@"0"];
        
    }
    [_MyStudentList reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];
    
}
//设置头的高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 53;
}

#pragma mark - 懒加载
-(NSMutableArray *)stuArray{
    if (!_stuArray) {
        _stuArray = [[NSMutableArray alloc]init];
    }
    return _stuArray;
}
-(NSMutableArray *)selectedArray{
    if (!_selectedArray) {
        _selectedArray = [[NSMutableArray alloc]init];
    }
    return _selectedArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIView *)noDataView{
    
    if (!_noDataView) {
        _noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, Navigation_Bar_Height+80+10, ScreenWidth, 60)];
        _noDataView.backgroundColor = [UIColor whiteColor];
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"NoData"];
        [_noDataView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_noDataView);
            make.top.mas_equalTo(50);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(100);
        }];
        
        UILabel * la = [[UILabel alloc]init];
        la.text = @"亲,暂时没有学员哦!";
        la.textColor = [UIColor darkGrayColor];
        la.textAlignment = NSTextAlignmentCenter;
        [_noDataView addSubview:la];
        [la mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_noDataView);
            [make.top.mas_equalTo(imageView.mas_bottom) setOffset:10];
            make.width.mas_equalTo(200);
        }];
    }
    return _noDataView;
}

@end
